package com.ufo.store.management.widget.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.method.DigitsKeyListener;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.ufo.store.management.R;

/**
 *  作者：taoli on 2017/10/11 0011 10:36
 *  
 */
public class ProtogenesisInputDialog {
    private Activity mActivity;
    private EditText mEditText;
    private String mTitle;
    private DigitsKeyListener mDigitsKeyListener;

    public ProtogenesisInputDialog(Activity activity, EditText editText, String title, DigitsKeyListener digitsKeyListener){
        mActivity = activity;
        mEditText = editText;
        mTitle = title;
        mDigitsKeyListener = digitsKeyListener;
        final View view = mActivity.getLayoutInflater().inflate(R.layout.dialog_custom_input, null, false);
        ((EditText) view.findViewById(R.id.input_et)).setText(mEditText.getText().toString());
        ((EditText) view.findViewById(R.id.input_et)).setSelection(mEditText.getText().toString().length());
        if( mDigitsKeyListener != null) {
            ((EditText) view.findViewById(R.id.input_et)).setKeyListener(mDigitsKeyListener);
        }
        AlertDialog alertDialog = new AlertDialog.Builder(mActivity).setTitle(mTitle).setView(view).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                hidenKeyBoard(view.findViewById(R.id.input_et));
                if( mListener != null ){
                    mListener.onPositiveButtonClick(((EditText) view.findViewById(R.id.input_et)).getText().toString());
                }
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                hidenKeyBoard(view.findViewById(R.id.input_et));
                if( mListener != null ){
                    mListener.onNegativeButtonClick();
                }
            }
        }).setCancelable(true).create();
        alertDialog.getWindow().setWindowAnimations(R.style.dialog_anim_center_style);
        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            public void onShow(DialogInterface dialog) {
                showKeyBoard(view.findViewById(R.id.input_et));
            }
        });
        alertDialog.show();
    }

    private void showKeyBoard(View v){
        InputMethodManager inputManager = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
    }

    private void hidenKeyBoard(View v) {
        InputMethodManager inputManager = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }

    private OnItemClick mListener = null;

    public void setItemClickListener(OnItemClick p) {
        mListener = p;
    }

    public interface OnItemClick {
        void onPositiveButtonClick(String inputString);
        void onNegativeButtonClick();
    }
}
